﻿using LiveChartsCore.SkiaSharpView.Extensions;
using LiveChartsCore;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;
using LiveChartsCore.SkiaSharpView;
using System.Collections.ObjectModel;
using 基于WPF的电缆采样数据分析系统.Tools.LangugeTool;
using 基于WPF的电缆采样数据分析系统.Models.FileLogs;
using 基于WPF的电缆采样数据分析系统.Models.Enums;
using Prism.Events;
using 基于WPF的电缆采样数据分析系统.MsgEvents.DeviceStatus;

namespace 基于WPF的电缆采样数据分析系统.ViewModels.DeviceStatus
{
    public class ColumnTwoUCViewModel : BindableBase
    {

        /// <summary>
        /// 图表数据
        /// </summary>
        public ObservableCollection<ISeries> Series { get; set; }


        /// <summary>
        /// 发布订阅
        /// </summary>
        private readonly IEventAggregator Aggregator;


        /// <summary>
        /// 构造器
        /// </summary>
        public ColumnTwoUCViewModel()
        {

            Aggregator = App.GetService<IEventAggregator>();
            Aggregator.GetEvent<MsgFileUsedStatus>().Subscribe(UpdatePie);


            // 更新饼图
            UpdatePie();
        }


        /// <summary>
        /// 更新饼图
        /// </summary>
        private void UpdatePie()
        {
            ObservableCollection<UsedFileLog> usedFileLogList = GlobalVariableEdit.UsedFileLogList;

            int ImportWavCount = usedFileLogList.Where(u => u.FileType == FileType.ImportWaveformFile).Count();
            int ProjectCount = usedFileLogList.Where(u => u.FileType == FileType.ProjectFile).Count();
            int ExportTaskCount = usedFileLogList.Where(u => u.FileType == FileType.ExportTaskListFile).Count();
            int ExportSingleWavCount = usedFileLogList.Where(u => u.FileType == FileType.ExportSingleWavformFile).Count();
            int ExportLogCount = usedFileLogList.Where(u => u.FileType == FileType.ExportLog).Count();

            int total = usedFileLogList.Count;


            if (total != 0)
            {

                // 初始化数据
                Series = new ObservableCollection<ISeries>
                {
                    

                        // 导入波形文件类型
                    new PieSeries<double>
                    {
                        Values = new double[] { ImportWavCount },
                        Name = LangugeConvertor.GetValue("ImportWaveformFileType"),
                        ToolTipLabelFormatter = (context) => string.Concat((Math.Round((float)ImportWavCount / (float)total, 2) * 100).ToString(), "%")
                    },
                    // 工程文件类型
                    new PieSeries<double>
                    {
                        Values = new double[] { ProjectCount },
                        Name = LangugeConvertor.GetValue("ProjectFileType"),
                        ToolTipLabelFormatter = (context) => string.Concat(Math.Round((float)ProjectCount / (float)total, 2) * 100, "%")
                    },
                    // 导出任务集合类型
                    new PieSeries<double>
                    {
                        Values = new double[] { ExportTaskCount },
                        Name = LangugeConvertor.GetValue("ExportTaskListFileType"),
                        ToolTipLabelFormatter = (context) => string.Concat(Math.Round((float)ExportTaskCount / (float)total, 2) * 100, "%")
                    },
                    // 导出单个波形文件类型
                    new PieSeries<double>
                    {
                        Values = new double[] { ExportSingleWavCount },
                        Name = LangugeConvertor.GetValue("ExportSingleWavformFileType"),
                        ToolTipLabelFormatter = (context) => string.Concat(Math.Round((float)ExportSingleWavCount / (float)total, 2) * 100, "%")
                    },
                    // 导出日志文件类型
                    new PieSeries<double>
                    {
                        Values = new double[] { ExportLogCount },
                        Name = LangugeConvertor.GetValue("TypeOfTheExportedLogFile"),
                        ToolTipLabelFormatter = (context) => string.Concat(Math.Round((float)ExportLogCount / (float)total, 2) * 100, "%")
                    }
                };

                // 显示饼图
                Aggregator.GetEvent<MsgIsFileUsed>().Publish();

                // 手动触发 CollectionChanged 事件
                RaisePropertyChanged("Series");
            }

            
        }
    }
}
